package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import manager.CustomerManager;
import manager.impl.CustomerManagerImpl;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vo.Customer;

@Controller
public class KfcController {
  CustomerManager cm = new CustomerManagerImpl();
  
  @RequestMapping("/welcome")
  public String welcome()
  {
	  return "welcome";
  }
  @RequestMapping("/regist")
  public String regist(String username,String password,String telphone,String address)
  {
	  Customer customer = new Customer();
	  customer.setUsername(username);
	  customer.setPassword(password);
	  customer.setTelphone(telphone);
	  customer.setAddress(address);
	  
	  if(cm.regist(customer))
//		  HttpSession s = req.getSession();
//	  s.setAttribute("customer",customer);
//	  try {
//		req.getRequestDispatcher("/helloword.html").forward(req, res);
//	} catch (ServletException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	} catch (IOException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
//	  return null;
	  return "redirect:login?username="+username+"&password="+password;
	  else
	  return "regist";
  }
  @RequestMapping("/login")
  public String login(String username,String password,HttpServletRequest res)
  {

	  Customer customer = cm.login(username, password);
	  if(customer!=null)
	  {
		  HttpSession s = res.getSession();
		  s.setAttribute("customer",customer);
		  return "WEB-INF/welcome";
		  }
	  else
		  return"login";
  }
}
